package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.service.CategoryService;
import net.zjitc.utils.APIResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService vueShopCategory;

    @GetMapping("/findAll")
    @CrossOrigin(origins = "*")
    public APIResponse<List<Category>> category(){
        List<Category> shops = vueShopCategory.findAll();
        APIResponse<List<Category>> api = new APIResponse<>();
        api.ok("查询成功",shops);
        return api;
    }

//    @GetMapping("/delete")
//    public String delete(Integer id,Model model){
//        vueShopCategory.delete(id);
//        return category(model);
//    }
@PostMapping("/delete")
@CrossOrigin(origins = "*")
public APIResponse<String> delete(@RequestBody Map<String, Integer> request) {
    // 从请求体中提取id
    System.out.println(request);
    Integer id = request.get("id");
    vueShopCategory.delete(id);
    APIResponse<String> api = new APIResponse<>();
    api.ok("删除成功", "");
    return api;
}

//    @PostMapping("/add")
//    public String insertShop(Category category, Model model) {
//        vueShopCategory.insertShop(category);
//        return category(model);
//    }
    @PostMapping("/add")
    @CrossOrigin(origins = "*")
    public APIResponse<String> insertShop(@RequestBody Category category) {
        APIResponse<String> api = new APIResponse<>();
        if(category.getPid()==null){
            category.setPid(0);
        }
        vueShopCategory.insertShop(category);

        api.ok("添加成功", "");
        return api;
    }
//    @GetMapping("/findById")
//    public String findById(Integer id,Model model){
//        Category shop = vueShopCategory.findById(id);
//        model.addAttribute("shop",shop);
//        List<Category> pCategory = vueShopCategory.findPid();
//        model.addAttribute("pCategory",pCategory);
//        return "update";
//    }
//    @PostMapping("update")
//    public String update(Category category,Model model){
//        vueShopCategory.update(category);
//        return category(model);
//    }
@GetMapping("/findById")
@CrossOrigin(origins = "*")
public APIResponse<Category> findById(@RequestParam Integer id){
    Category shop = vueShopCategory.findById(id);
    APIResponse<Category> api = new APIResponse<>();
    api.ok("查询单个成功", shop);
    return api;
}
    @PostMapping("update")
    @CrossOrigin(origins = "*")
    public APIResponse<String> update(@RequestBody Category category){
        vueShopCategory.update(category);
        APIResponse<String> api = new APIResponse<>();
        api.ok("更新成功", "");
        return api;
    }
    @GetMapping("/findPid")
    public String findPid(Model model){
        List<Category> pCategory = vueShopCategory.findPid();
        model.addAttribute("pCategory",pCategory);
        return "add";
    }
    @PostMapping("/findByName")
    public String findByName(@Param("name") String name,Model model){
        List<Category> shops = vueShopCategory.findByName(name);
        model.addAttribute("shops",shops);
        return "list";
    }
}
